package com.gzkemays.demo.config;

import com.gzkemays.demo.config.filter.JwtAuthorizationFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author gzkemays
 * @date 2021/3/18 13:24
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
  PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("gzkemays")
        .password(new BCryptPasswordEncoder().encode("gzkemays"))
        .roles("admin")
        .and()
        .withUser("user")
        .password(new BCryptPasswordEncoder().encode("123"))
        .roles("user");
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
    http.formLogin();
    http.addFilter(new JwtAuthorizationFilter(authenticationManager()));
    //    http.requestMatchers()
    //        .antMatchers("/oauth/**")
    //        .and()
    //        .authorizeRequests()
    //        .antMatchers("/oauth/**")
    //        .authenticated()
    //        .and()
    //        .formLogin();
    //        .and()
    //        .addFilter(new JwtAuthorizationFilter(authenticationManager()))
    //        .sessionManagement()
    //        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  }

  //  @Override
  //  public void configure(WebSecurity web) {
  //    web.ignoring().antMatchers("/oauth/**");
  //  }
}
